// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

/**
 * Created by eran on 14-12-21.
 */
package gui.component.fileOperate
{
    import com.bit101.components.PushButton;

    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.filesystem.File;
    import flash.text.TextField;

    import gui.manger.GUIManger;
    import gui.utils.ConfirmBox;
    import gui.utils.GUIUtils;

    public class FileOperateLayer
    {
        private var mFileLayerMc:MovieClip;
        private var mManger:GUIManger;

        private var mOperBtn:PushButton;
        private var mSaveBtn:PushButton;
        private var mFilePathTF:TextField;

        private var mCurrentFile:File;

        public function FileOperateLayer()
        {
        }

        //=================================//
        //=== Initialize & Dispose ========//
        //=================================//

        public function initialize(_fileLayerMc:MovieClip, _manger:GUIManger):void
        {
            mFileLayerMc = _fileLayerMc;
            mManger = _manger;

            mFilePathTF = mFileLayerMc["filePathTF"];
            mFilePathTF.visible = false;

            mOperBtn = new PushButton(null, 0, 0, "Open", onClickOpenBtn);
            GUIUtils.replaceTargetWithPH(mOperBtn, mFileLayerMc["openBtnPH"]);

            mSaveBtn = new PushButton(null, 0, 0, "Save", onClickSaveBtn);
            GUIUtils.replaceTargetWithPH(mSaveBtn, mFileLayerMc["saveBtnPH"]);
        }

        //=================================//
        //====== Public Function ==========//
        //=================================//

        public function refresh(_currentFile:File):void
        {
            mCurrentFile = _currentFile;
            if (mCurrentFile != null)
            {
                mFilePathTF.visible = true;
                mFilePathTF.text = mCurrentFile.nativePath;
            }
            else
            {
                mFilePathTF.visible = false;
            }
        }

        //=================================//
        //====== Private Function =========//
        //=================================//

        private function onClickOpenBtn(e:MouseEvent):void
        {
            if (mCurrentFile != null)
            {
                ConfirmBox.instance.ask("打开新的会导致当前丢失\n是否继续?", onConfirmOpenNewFile);
            }
            else
            {
                mManger.onClickOpenBtn();
            }
        }

        private function onClickSaveBtn(e:MouseEvent):void {mManger.onClickSaveBtn();}

        //================
        //==Event Confirm
        //================

        private function onConfirmOpenNewFile():void
        {
            mManger.onClickOpenBtn();
        }


    }
}
